<script lang="ts" setup>
import type { ResumeListItem } from "./apis/type"
import { Plus } from "@element-plus/icons-vue"
import { ElButton } from "element-plus"
import { onMounted, ref, watch } from "vue"
import { useRouter } from "vue-router"
import ResumeDetail from "@/pages/internship/resume/components/ResumeDetail.vue"
import ResumeList from "@/pages/internship/resume/components/ResumeList.vue"
import { useResumeStore } from "@/pinia/stores/resume"

defineOptions({ name: "ResumePage" })

// 获取路由实例
const router = useRouter()

// 获取简历store实例
const resumeStore = useResumeStore()

// 选中的简历ID
const selectedId = ref<number | null>(null)

// 处理选中简历
function onSelect(item: ResumeListItem) {
  selectedId.value = item.resumeId
  resumeStore.fetchResumeDetail(item.resumeId)
}

// 处理添加简历
function handleAddResume() {
  router.push({
    path: "/internship/resume/add"
  })
}

// 监听列表数据变化，当列表加载完成后自动选择第一个项目
watch(
  () => resumeStore.resumeList,
  (newList) => {
    // 只有当没有选中项且列表不为空时才自动选择第一个
    if (newList && newList.length > 0 && selectedId.value === null) {
      const firstItem = newList[0]
      onSelect(firstItem)
    }
  },
  { immediate: true }
)

// 处理加载更多
async function handleLoadMore() {
  await resumeStore.loadMore()
}

// 页面加载时获取数据
onMounted(async () => {
  // 重置页码到第一页
  resumeStore.updateQueryParams({ page: 1 })
  await resumeStore.fetchUserResumes()
})
</script>

<template>
  <div class="resume-page">
    <div class="resume-banner">
      <div class="page-container">
        <div class="banner-controls">
          <h1 class="page-title">
            我的简历
          </h1>
          <div class="action-buttons">
            <ElButton type="primary" size="large" @click="handleAddResume">
              <el-icon><Plus /></el-icon>
              新建简历
            </ElButton>
          </div>
        </div>
      </div>
    </div>

    <div class="page-container">
      <div class="pane">
        <ResumeList
          :items="resumeStore.resumeList"
          :loading="resumeStore.loading"
          :total="resumeStore.total"
          :selected-id="selectedId"
          @select="onSelect"
          @load-more="handleLoadMore"
          :max-width="400"
        />
        <div class="pane-right">
          <ResumeDetail />
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
.resume-page {
  background-color: #f7f8fa;
  min-height: calc(100vh - 64px);
}

.resume-banner {
  position: sticky;
  top: 0;
  z-index: 9;
  background: linear-gradient(90deg, #f0f6ff 0%, #f7fbff 100%);
  border-bottom: 1px solid #eef2f7;
}

.page-container {
  max-width: 1280px;
  margin: 0 auto;
  padding: 12px 16px;
}

.banner-controls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.page-title {
  font-size: 20px;
  font-weight: 600;
  color: #1f2937;
  margin: 0;
}

.action-buttons {
  display: flex;
  gap: 8px;
  align-items: center;
}

/* 列表与右侧面板 */
.pane {
  display: flex;
  grid-template-columns: minmax(0, 1fr) 420px;
  gap: 10px;
  margin-top: 16px;
  align-items: flex-start;
}

.pane-right {
  width: 100%;
  background: #fff;
  border: 1px solid #eef2f7;
  border-radius: 12px;
  padding: 12px;
  min-height: 240px;
}

/* 响应式布局 */
@media (max-width: 768px) {
  .pane {
    flex-direction: column;
    grid-template-columns: 1fr;
  }

  .pane-right {
    min-height: auto;
  }
}

/* 空状态样式 */
.pane-right .empty-detail {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: #6b7280;
  min-height: 200px;
}
</style>
